<template>
  <div class="rightList">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>权限列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 添加角色 -->
    <el-button type="primary" @click="addRoleShow = true">添加角色</el-button>
    <!-- 添加角色对话框 -->
    <el-dialog title="添加角色" :visible.sync="addRoleShow">
      <el-form :model="addRoleForm" :label-width="'100px'" ref="addRoleForm" :rules="rules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addRoleForm.roleName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addRoleForm.roleDesc" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addRoleShow = false">取 消</el-button>
        <el-button type="primary" @click="addRoles">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 数据列表 -->
    <el-table :data="roleList" style="width: 100% ">
      <!-- 展开角色区域 -->
      <el-table-column type="expand">
        <template slot-scope="scope">
          <el-row
            v-for="first in scope.row.children"
            :key="first.id"
            style="margin-bottom:10px;border-bottom:1px #ccc dashed"
          >
            <el-col :span="4">
              <el-tag closable @close="delRole(scope.row,first.id)">{{first.authName}}</el-tag>
            </el-col>
            <el-col :span="20">
              <el-row v-for="tow in first.children" :key="tow.id">
                <el-col :span="4">
                  <el-tag
                    closable
                    type="success"
                    @close="delRole(scope.row,tow.id)"
                  >{{tow.authName}}</el-tag>
                </el-col>
                <el-col :span="20">
                  <el-tag
                    closable
                    type="danger"
                    v-for="three in tow.children"
                    :key="three.id"
                    style="margin-right:4px;margin-bottom:6px"
                    @close="delRole(scope.row,three.id)"
                  >{{three.authName}}</el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24" v-show="scope.row.children.length ===0">没有数据，请添加数据</el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" width="50"></el-table-column>
      <el-table-column label="角色名称" prop="roleName"></el-table-column>
      <el-table-column label="描述" prop="roleDesc"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" content="编辑角色" placement="top">
          <el-button type="primary" icon="el-icon-edit" @click='eidtShow(scope.row)'></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="授权角色" placement="top">
          <el-button type="primary" icon="el-icon-share" @click="rightShow(scope.row)"></el-button>
          </el-tooltip>
          <el-tooltip class="item" effect="dark" content="删除角色" placement="top">
          <el-button type="primary" icon="el-icon-delete" @click="delRoles(scope.row.id)"></el-button>
          </el-tooltip>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分配权限对话框 -->
    <el-dialog title="提示" :visible.sync="rightdialog" width="30%">
      <el-tree
        ref="mytree"
        :data="rightList"
        show-checkbox
        node-key="id"
        :default-expand-all="true"
        :default-checked-keys="checkedArr"
        :props="defaultProps"
      ></el-tree>
      <span slot="footer" class="dialog-footer">
        <el-button @click="rightdialog = false">取 消</el-button>
        <el-button type="primary" @click="grantRightSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑角色对话框 -->
    <el-dialog title="添加角色" :visible.sync="editRoleShow">
      <el-form :model="editRoleForm" :label-width="'100px'" ref="editRoleForm" :rules="rules">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="editRoleForm.roleName" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="editRoleForm.roleDesc" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editRoleShow = false">取 消</el-button>
        <el-button type="primary" @click="editRoles">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getAllRoleLlist,
  delRightByRoleId,
  grantRightForRole,
  addRole,
  delRole,
  editRole
} from '@/api/role.js'
import { right } from '@/api/right.js'
export default {
  data () {
    return {
      // 编辑角色
      editRoleForm: {
        roleName: '',
        roleDesc: '',
        id: ''
      },
      editRoleShow: false,
      // 添加角色
      addRoleForm: {
        roleName: '',
        roleDesc: ''
      },
      addRoleShow: false,
      rigid: '',
      roleList: [],
      rightdialog: false,
      checkedArr: [],
      rightList: [], // 树形结构展示的数据
      defaultProps: {
        children: 'children',
        label: 'authName'
      },
      rules: {
        roleName: [
          { required: true, message: '请输入名称', trigger: 'change' }
        ],
        roleDesc: [
          { required: true, message: '请输入职业名称', trigger: 'change' }
        ]
      }
    }
  },
  mounted () {
    this.init()
  },
  methods: {
    // 编辑角色
    eidtShow (row) {
      this.editRoleShow = true
      console.log(row)
      this.editRoleForm.roleName = row.roleName
      this.editRoleForm.roleDesc = row.roleDesc
      this.editRoleForm.id = row.id
    },
    editRoles () {
      this.$refs.editRoleForm.validate((valid) => {
        if (valid) {
          editRole(this.editRoleForm)
            .then((res) => {
              if (res.data.meta.status === 200) {
                this.$message({
                  type: 'success',
                  message: '修改成功'
                })
                this.editRoleShow = false
                this.init()
              }
            }).catch((err) => {
              console.log(err)
            })
        }
      })
    },
    // 删除角色
    delRoles (id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          delRole(id).then(res => {
            if (res.data.meta.status === 200) {
              this.$message({
                type: 'success',
                message: '删除成功!'
              })
              this.init()
            }
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 添加角色
    addRoles () {
      this.$refs.addRoleForm.validate(vali => {
        if (vali) {
          addRole(this.addRoleForm)
            .then(res => {
              console.log(res)
              if (res.data.meta.status === 201) {
                this.$message({
                  type: 'success',
                  message: res.data.meta.msg
                })
                // 隐藏对话框
                this.addRoleShow = false
                // 清空文本域内容
                this.$refs.addRoleForm.resetFields()
                this.init()
              }
            })
            .catch(err => {
              console.log(err)
            })
        } else {
          return false
        }
      })
    },
    // 给角色授权
    grantRightSubmit () {
      var arr = this.$refs.mytree.getCheckedNodes()
      console.log(arr)
      var temp = []
      for (let i = 0; i < arr.length; i++) {
        // 将id跟pid拼接在一起
        temp.push(arr[i].id + ',' + arr[i].pid)
      }
      // console.log(temp)
      // 将temp数组转换为逗号隔开的字符串格式
      var str = temp.join(',')
      // console.log(str)
      // 将str转换为数组去重
      var arr2 = str.split(',')
      // console.log(arr2)
      var finalArr = [...new Set(arr2)]
      console.log(finalArr.join(','))
      grantRightForRole(this.rigid, finalArr.join(','))
        .then(res => {
          if (res.data.meta.status === 200) {
            this.$message({
              type: 'success',
              message: res.data.meta.msg
            })
            this.rightdialog = false
            this.init()
          }
        })
        .catch(err => {
          console.log(err)
        })
    },
    // 分配角色权限数据渲染
    rightShow (row) {
      this.rightdialog = true
      this.rigid = row.id
      right('tree').then(res => {
        console.log(res)
        if (res.data.meta.status === 200) {
          this.rightList = res.data.data
        }
      })
      this.checkedArr.length = 0
      row.children.forEach(first => {
        if (first.children && first.children.length > 0) {
          first.children.forEach(tow => {
            if (tow.children && tow.children.length > 0) {
              tow.children.forEach(three => {
                this.checkedArr.push(three.id)
              })
            }
          })
        }
      })
    },
    // 删除角色权限
    delRole (row, rightId) {
      delRightByRoleId(row.id, rightId).then(res => {
        console.log(res)
        if (res.data.meta.status === 200) {
          this.$message({
            type: 'success',
            message: res.data.meta.msg
          })
          row.children = res.data.data
        }
      })
    },
    init () {
      getAllRoleLlist()
        .then(res => {
          // console.log(res)
          if (res.data.meta.status === 200) {
            this.roleList = res.data.data
          }
        })
        .catch(err => {
          console.log(err)
        })
    }
  }
}
</script>
<style lang="less" scoped>
</style>
